package com.toman.ui.work.base;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Enum.BaseEnum;
import com.toman.Enum.PermEnum;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.SysUserEntity;
import com.toman.R;
import com.toman.Util.DefaultAcitonCallback;
import com.toman.Util.DialogUtils;
import com.toman.Util.IntentUtils;
import com.toman.Util.JsonUtils;
import com.toman.Util.PermissonUtils;
import com.toman.Util.SPUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.ItemListBean;
import com.toman.toolbar.ToolBarActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

public class UserInfActivity extends ToolBarActivity {


    String id;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_no)
    TextView tvTitleNo;
    @BindView(R.id.tv_title2)
    TextView tvTitle2;
    @BindView(R.id.tv_title3)
    TextView tvTitle3;
    @BindView(R.id.tv_title4)
    TextView tvTitle4;
    @BindView(R.id.cv_base)
    CardView cvBase;
    @BindView(R.id.tv_content1)
    TextView tvContent1;
    @BindView(R.id.tv_content2)
    TextView tvContent2;
    @BindView(R.id.tv_content3_title)
    TextView tvContent3Title;
    @BindView(R.id.tv_content3)
    TextView tvContent3;
    @BindView(R.id.tv_content4)
    TextView tvContent4;
    @BindView(R.id.tv_content5)
    TextView tvContent5;
    @BindView(R.id.cv_more)
    CardView cvMore;
    SysUserEntity sysUserEntity;
    @BindView(R.id.tv_title2_name)
    TextView tvTitle2Name;
    @BindView(R.id.tv_title5)
    TextView tvTitle5;
    @BindView(R.id.tv_content6)
    TextView tvContent6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_people_inf);
        ButterKnife.bind(this);
        initToolBar("人员详情");
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        get();
    }

    private void initView() {
        id = getIntent().getStringExtra("id");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_edit_delete, menu);
        MenuItem menuItem = menu.findItem(R.id.menu_delete);
        if (!PermissonUtils.contains(this, BaseEnum.USER.has(PermEnum.DELETE))) {
            menuItem.setVisible(false);
        }

        menuItem = menu.findItem(R.id.menu_edit);
        if (!PermissonUtils.contains(this, BaseEnum.USER.has(PermEnum.EDIT))) {
            menuItem.setVisible(false);
        }
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (sysUserEntity == null) {
                    ToastUtil.makeText(getApplicationContext(), "等待加载人员信息");
                }
                switch (item.getItemId()) {
                    case R.id.menu_edit:
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("bean", sysUserEntity);
                        bundle.putString("type", "2");

                        IntentUtils.startActivity(UserInfActivity.this, UserEditActivity.class, bundle);
                        break;
                    case R.id.menu_delete:
                        deleteDialog();
                        break;
                }
                return false;
            }
        });

        return true;
    }

    public void deleteDialog() {

        //LayoutInflater是用来找layout文件夹下的xml布局文件，并且实例化

        new AlertDialog.Builder(this)
                //对话框的标题
                .setTitle("确定要删除人员吗？")
                //设定显示的View
                //对话框中的“登陆”按钮的点击事件
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        delete();
                    }

                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                // 设置dialog是否为模态，false表示模态，true表示非模态
                .setCancelable(true)
                //对话框的创建、显示
                .create().show();

    }


    private void delete() {
        Map<String, String> map = new HashMap<String, String>();

        map.put("userId", sysUserEntity.getUserId() + "");
        String str = JsonUtils.obj2JsonString(map);
        OkHttpUtils
                .post(NewBaseUrl.user_delete_post + sysUserEntity.getUserId())
                .headers("token", token)
                .postJson(str)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "删除人员: " + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());

                        if (code == 0) {
                            ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
                            finish();
                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });
    }

    public void get() {
        String token = (String) SPUtils.get(getApplicationContext(), "token", "");
        OkHttpUtils
                .get(NewBaseUrl.user_info_get + id)
                .params("token", token)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "人员信息: " + result);

                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());
                        if (code == 0) {
                            SysUserEntity bean = (SysUserEntity) JSON.parseObject(jsonObj.getString("user"), SysUserEntity.class);
                            sysUserEntity = bean;
                            tvTitle.setText(bean.getUsername());
                            tvTitleNo.setText(bean.getUserNum() + "");
                            tvTitle2.setText(bean.getAppRoleIdListName());
                            tvTitle3.setText(bean.getDepartmentName());
                            tvTitle4.setText(bean.getMobile());
                            tvTitle5.setText(bean.getRealName());
                            tvContent2.setText(bean.getDepartmentNum());
                            tvContent4.setText(bean.getEmail());
                            tvContent5.setText(bean.getPostInformation());
                            String[] str = new String[]{"禁用", "在职", "离职"};
                            tvContent6.setText(str[bean.getStatus()]);
                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });
    }
}
